package com.woniu.shops.controller;
import com.woniu.common.constans.Constants;
import com.woniu.common.pojo.Flower;
import com.woniu.common.pojo.Order;
import com.woniu.common.pojo.Shops;
import com.woniu.common.pojo.User;
import com.woniu.common.utils.TokenUtil;
import com.woniu.common.vo.AjaxResult;
import com.woniu.common.vo.Orderss;
import com.woniu.shops.service.OrdersService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/Order")
@CrossOrigin
public class OrderController {
    @Resource
    private OrdersService ordersService;

    @RequestMapping("getOrder")
    public AjaxResult getOrder(HttpServletRequest request, @Param("pageNum")Integer pageNum, @Param("pageSize")Integer pageSize) {
        Shops shops = TokenUtil.getObj(request, Shops.class);
        List<Order> order = ordersService.getOrder(shops);
        ArrayList<Order> orders = new ArrayList<>();
        Integer pageNums = (pageNum-1)*pageSize;
        Integer is = order.size() - pageSize - pageNums;
        if(is < 0){
            for (int i = pageNums; i < order.size(); i++) {
                orders.add(order.get(i));
            }
        }else {
            for (int i = pageNums; i < pageNums+pageSize; i++) {
                orders.add(order.get(i));
            }
        }
        Orderss orderss = new Orderss(orders,order.size());
        return AjaxResult.success("订单如下",orderss);
    }
    @RequestMapping("updateOrder")
    public AjaxResult updateOrder(@RequestBody Order order) {
        System.out.println(order+"_____++++");
        ordersService.updateOrder(order);
        return AjaxResult.success("支付状态修改完毕");
    }
    @RequestMapping("flushed")
    public AjaxResult flushed(HttpServletRequest request) {
        Shops obj = TokenUtil.getObj(request, Shops.class);
        ordersService.Flusheds(obj);
        return AjaxResult.success("支付状态修改完毕");
    }
    @RequestMapping("deleteOrder")
    public AjaxResult deleteOrder(@RequestBody Order order) {
        System.out.println(order+"_____++++");
        ordersService.deleteOrder(order);
        return AjaxResult.success("支付状态修改完毕");
    }
    @RequestMapping("deleteMoreOrder")
    public AjaxResult deleteOrder(@RequestBody Integer[] selectedOrderIds,HttpServletRequest request) {
        Shops obj = TokenUtil.getObj(request, Shops.class);
        ordersService.deleteOrders(selectedOrderIds,obj);
        return AjaxResult.success("订单删除完毕");
    }
    @RequestMapping("findOrder")
    public AjaxResult findOrder(@Param("username") String username, @Param("address") String address, HttpServletRequest request,@Param("pageNum")Integer pageNum, @Param("pageSize")Integer pageSize) {
        Shops obj = TokenUtil.getObj(request, Shops.class);
        Order order = new Order();
        order.setAddress(address);
        User user = new User();
        user.setUsername(username);
        order.setUser(user);
        order.setShops(obj);
        List<Order> orderss = ordersService.getOrders(order);
        System.out.println(pageNum+"________"+pageSize);
        ArrayList<Order> orders = new ArrayList<>();
        Integer pageNums = (pageNum-1)*pageSize;
        Integer is = orderss.size() - pageSize - pageNums;
        if(is < 0){
            for (int i = pageNums; i < orderss.size(); i++) {
                orders.add(orderss.get(i));
            }
        }else {
            for (int i = pageNums; i < pageNums+pageSize; i++) {
                orders.add(orderss.get(i));
            }
        }
        Orderss ordersss = new Orderss(orders,orderss.size());
        return AjaxResult.success("订单如下",ordersss);
    }

    @RequestMapping("isUpdate")
    public AjaxResult isUpdate(@Param("oid")String oid, HttpServletRequest request,@Param("fid")Integer fid,@Param("state")Integer state) {
        Shops obj = TokenUtil.getObj(request, Shops.class);
        Order order = new Order();
        order.setShops(obj);
        Flower flower = new Flower();
        flower.setFid(fid);
        order.setFlower(flower);
        order.setOid(oid.toString());
        order.setState(state);
        ordersService.updateOrder(order);
        return AjaxResult.success("退款申请审批成功");
    }

}
